<template>
  <div class="header">
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <a href="javascript:;">奕鼎通商城</a>
          <a href="javascript:;">MIUI</a>
          <a href="javascript:;">云服务</a>
          <a href="javascript:;">金融</a>
        </div>
        <div class="topbar-user">
          <a href="javascript:;" v-if="username">{{ username }}</a>
          <a href="javascript:;" v-if="!username" @click="login">登陆</a>
          <a href="javascript:;" v-if="!username">注册</a>
          <a href="javascript:;" v-if="username">我的订单</a>
          <a href="javascript:;" v-if="username" @click="exit">退出</a>
          <a href="javascript:;" class="my-cart" @click="goToCart"
            ><span class="icon-cart"></span>购物车({{ cartCount }})</a
          >
        </div>
      </div>
    </div>
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <a href="/index"></a>
        </div>
        <div class="header-menu">
          <div class="item-menu">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li
                  class="product"
                  v-for="(item, index) in phoneList"
                  :key="index"
                >
                  <a :href="'/product/' + item.pid" target="_blank">
                    <div class="pro-img">
                      <img :src="item.main_image" :alt="item.name" />
                    </div>
                    <div class="pro-name">{{ item.pro_name }}</div>
                    <div class="pro-price">{{ item.price | currency }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>电视</span>
            <div class="children"></div>
          </div>
          <div class="item-menu">
            <span>笔记本</span>
            <div class="children"></div>
          </div>
          <div class="item-menu">
            <span>家电</span>
            <div class="children"></div>
          </div>
          <div class="item-menu">
            <span>路由器</span>
            <div class="children"></div>
          </div>
        </div>
        <div class="search">
          <div class="wrapper">
            <input type="text" name="keyword" />
            <a href="javascript:;"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "navheader",
  data() {
    return {
      phoneList: [],
    };
  },

  mounted() {
    this.getProList();
  },
  computed: {
    // username() {
    //   return this.$store.state.username;
    // },
    // cartCount() {
    //   return this.$store.state.cartCount;
    // },

    //等价于
    ...mapState(["username", "cartCount"]),
  },
  filters: {
    currency(val) {
      if (!val) return "0.00";
      return "¥" + parseFloat(val).toFixed(2) + "元";
    },
  },
  methods: {
    exit() {
      this.$cookie.delete("uid");
      location.reload();
    },
    login() {
      this.$router.push("/login");
    },
    goToCart() {
      this.$router.push("/cart");
    },
    getProList() {
      this.axios
        .get("/getpros", {
          params: {
            cateid: "100001",
            pagesize: 6,
          },
        })
        .then((resp) => {
          this.phoneList = resp;
        })
        .catch((resp) => {
          console.log(resp);
        });
    },
  },
};
</script>

<style lang="scss">
@import "./../assets/scss/base.scss";
@import "./../assets/scss/mixin.scss";
@import "./../assets/scss/config.scss";
.header {
  .nav-topbar {
    height: 39px;
    line-height: 39px;
    background-color: #333;
    color: #b0b0b0;
    .container {
      @include flex;
      a {
        display: inline-block;
        color: #b0b0b0;
        margin-right: 17px;
      }
      .my-cart {
        width: 110px;
        background-color: #ff6600;
        text-align: center;
        color: #fff;
      }
    }
  }
  .nav-header {
    .container {
      position: relative;
      height: 112px;
      @include flex;


      .header-menu {
        display: inline-block;
        padding-left: 209px;
        width: 643px;
        .item-menu {
          display: inline-block;
          color: #333;
          font-weight: bold;
          font-size: 16px;
          line-height: 112px;
          margin-right: 25px;
          span {
            cursor: pointer;
          }
          &:hover {
            color: $colorA;
            .children {
              opacity: 1;
              height: 220px;
            }
          }
          .children {
            position: absolute;
            top: 112px;
            left: 0px;
            width: 1226px;
            height: 0px;
            border-top: 1px solid #e5e5e5;
            overflow: hidden;
            transition: height 0.5s;
            z-index: 10;
            opacity: 0;
            .product {
              float: left;
              width: 16.6%;
              height: 220px;
              font-size: 12px;
              line-height: 12px;
              text-align: center;
              z-index: 10px;
              background-color: #fff;
              position: relative;
              &:before {
                content: "";
                position: absolute;
                top: 28px;
                right: 0px;
                border-left: 1px solid $colorF;
                height: 100px;
                width: 1px;
              }
              &:last-child:before {
                display: none;
              }
              img {
                width: auto;
                height: 111px;
                margin-top: 26px;
              }
              a {
                display: inline-block;
              }
              .pro-img {
                height: 137px;
              }
              .pro-name {
                font-weight: bold;
                margin-top: 19px;
                margin-bottom: 8px;
                color: $colorB;
              }
              .pro-price {
                color: $colorA;
              }
            }
          }
        }
      }
      .search {
        width: 339px;
        border: 1px solid #e0e0e0;
        .wrapper {
          height: 50px;
          display: flex;
          align-items: center;
          input {
            border: 0px;
            border-right: 1px solid #e0e0e0;
            width: 264px;
            height: 46px;
            padding-left: 10px;
            outline: none;
          }
          a {
            @include bgImg(18px, 18px, "/imgs/icon-search.png");
            margin-left: 17px;
          }
        }
      }
    }
  }
}
</style>